package com.platform.utils;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

/**
 * @author pjx
 */
@Data
@NoArgsConstructor
public class Result {

    // 返回结果正确代码
    public static final String SUCCESS_CODE = "1";
    // 返回结果正确描述
    public static final String SUCCESS_DESC = "success";
    // 返回结果错误代码
    public static final String ERROR_CODE = "0";
    // 返回结果默认错误描述
    public static final String ERROR_DESC = "error";

    private static final Result RESULT_SUCCESS = new Result(SUCCESS_CODE, SUCCESS_DESC);

    /**
     * 返回结果
     */
    private String code;

    /**
     * 返回结果描述
     */
    private String des;

    public Result(String resultCode, String resultDes) {
        this.code = resultCode;
        this.des = resultDes;
    }

    public static Result success() {
        return RESULT_SUCCESS;
    }

    /**
     * errorResult:返回错误结果.<br/>
     *
     * @return
     */
    public static Result error() {
        return new Result(ERROR_CODE, ERROR_DESC);
    }

    /**
     * errorResult:返回自定义错误描述结果.<br/>
     *
     * @param errorDesc
     * @return
     */
    public static Result error(String errorDesc) {
        return new Result(ERROR_CODE, errorDesc);
    }

    public static Result error(String code, String errorDesc) {
        if (StringUtils.isEmpty(code)) {
            code = ERROR_CODE;
        }
        return new Result(code, errorDesc);
    }

    public static Result success(String succDesc) {
        return new Result(SUCCESS_CODE, succDesc);
    }
}
